package com.whli.jee.system.controller;

import com.whli.jee.common.share.entity.ResponseBean;
import com.whli.jee.common.web.controller.BaseController;
import com.whli.jee.common.web.service.IBaseService;
import com.whli.jee.system.entity.SysUserRole;
import com.whli.jee.system.service.ISysUserRoleService;
import com.whli.jee.system.vo.SysUserRoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>用户角色关系API</p>
 * @author whli
 * @version 1.0.0
 * @date 2019/7/24 14:43
 */
@RestController
@RequestMapping(value = "/system/sysUserRole")
@Api(description = "用户角色关系API")
public class SysUserRoleController extends BaseController<SysUserRole, SysUserRoleVO> {

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Override
    public IBaseService<SysUserRole> getService() {
        return sysUserRoleService;
    }

    @PostMapping(value = "/listByUser")
    @ApiOperation("根据用户查询角色")
    public List<String> listByUser(@RequestBody SysUserRole entity, HttpServletRequest request) throws Exception{
        return sysUserRoleService.listByUser(entity.getUserId());
    }

    @PostMapping(value = "/grant")
    @ApiOperation("给用户授权角色")
    public ResponseBean grant(@RequestBody SysUserRoleVO entity, HttpServletRequest request) throws Exception{
        ResponseBean bean = new ResponseBean();
        int rows = sysUserRoleService.grant(entity);
        if (rows > 0){
            bean.setCode("0");
            bean.setMessage("授权角色成功");
        }
        return bean;
    }
}
